package com.example.springbootdemo.controller.jsp;

import com.example.springbootdemo.bo.PageUtils;
import com.example.springbootdemo.entity.MallProductSpec;
import com.example.springbootdemo.pojo.Person;
import com.example.springbootdemo.service.ProductServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @Auther: zyb
 * @Date: 2018/10/29 14:34
 * @Description: jsp页面使用
 */

//返回的是页面时，使用此注解，使用RESTController时会解析为json字符串
@Controller
public class JSPController {

    @Autowired
    private ProductServiceImpl productService;

    @RequestMapping("/")
    public String index(Model model) {
//        List<Person> peopleList = new ArrayList<Person>();
//        peopleList.add(new Person("zyb", 22, "上海", "男"));
//        peopleList.add(new Person("lhb", 23, "北京", "女"));
//        peopleList.add(new Person("swb", 32, "成都", "男"));
//        Person person = new Person("zyb", 22, "上海", "男");
//        List<Integer> integers = new ArrayList<>();
//        integers.add(11);
//        integers.add(12);
//        integers.add(13);
//        integers.add(14);
//        integers.add(15);
//        model.addAttribute("integers", integers);
//        model.addAttribute("person", person);
//        model.addAttribute("peopleList", peopleList);
        return "layui/common";
    }

    @RequestMapping("/getProduct")
    public String getProduct(Model model) {
        List<Person> peopleList = new ArrayList<Person>();
        peopleList.add(new Person("zyb111", 11122, "111上12海", "12男"));
        peopleList.add(new Person("lhb111", 111123, "111北京", "女12"));
        peopleList.add(new Person("sw111b", 31112, "111成都", "男12"));
        Person person = new Person("z111yb", 11122, "111上海", "1212男");
        List<Integer> integers = new ArrayList<>();
        integers.add(11111);
        integers.add(1122);
        integers.add(12123);
        integers.add(1122124);
        integers.add(112125);
        model.addAttribute("integers", integers);
        model.addAttribute("person", person);
        model.addAttribute("peopleList", peopleList);
        return "layui/getProduct";
    }

    @ResponseBody
    @GetMapping("/getSpecList")
    public PageUtils getSpecList(Model model, @RequestParam("page") Integer startPage, @RequestParam("limit") Integer pageSize) {

        PageUtils<List<MallProductSpec>> pageUtils = new PageUtils<>();
        List<MallProductSpec> specList = productService.getSpecList(startPage, pageSize);
        pageUtils.setData(specList);
        pageUtils.setCode(0);
        Integer count = productService.getCount();
        pageUtils.setCount(count);
        pageUtils.setPages((int) Math.ceil(count / specList.size()));//粗略的总页数
        return pageUtils;
    }

    @ResponseBody
    @PostMapping("/upload")
    public PageUtils upload(HttpServletRequest request, HttpServletResponse response, @RequestParam("file") MultipartFile file) {
        PageUtils pageUtils = new PageUtils();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        if (file != null) {
            try {
                file.transferTo(new File("c:\\tmp\\" + file.getOriginalFilename()));
                pageUtils.setCode(0);
            } catch (IOException e) {
                e.printStackTrace();
                pageUtils.setCode(1);
            }
        }
        return pageUtils;
    }

}
